package com.hua.api.modules.express.util;

import com.alibaba.fastjson.JSONArray;
import com.hua.api.core.constants.Status;
import com.hua.api.core.util.StringUtil;
import com.hua.api.modules.express.domain.ExpressOrder;
import com.hua.api.modules.express.domain.ShopInfoExpress;
import com.hua.api.modules.order.domain.OrderInfo;
import com.hua.api.modules.order.domain.ShopInfo;

import java.util.Date;

public class ExpressOrderUtil {


    public static ExpressOrder getSelfExpressOrder(String expressCode, String expressName,OrderInfo order, ShopInfo shopInfo) {
        ExpressOrder o = new ExpressOrder();
        o.setOrderId(order.getId());
        o.setCreatedTime(new Date());
        o.setAppointType(0);
        o.setDeliveryType(1); // 1.帮我送 2.帮我取
        o.setExpressCode(expressCode);
        o.setExpressName(expressName);

        o.setFromAddress(shopInfo.getShopAddress());
        o.setFromMobile(shopInfo.getPhoneNumber());
        o.setFromLongitude(shopInfo.getLongitude());
        o.setFromLatitude(shopInfo.getLatitude());

        o.setOrderTime(new Date());
        o.setToMobile(order.getRecipientPhone());
        o.setToAddress(order.getRecipientAddress());
        o.setToLatitude(order.getLatitude());
        o.setToLongitude(order.getLongitude());
        o.setToReceiveName(order.getRecipientName());
        o.setToGeoType(order.getGeoType());
        o.setGoodType(7);
        o.setWeight(1d);
        o.setFee(0); //费用 (分） 下同
        o.setAddFee(0); // 小费
        o.setDistance(0); //距离 (米)
        o.setTotalAmount(0); // 总金额
        o.setCouponFee(0); // 优惠金额
        o.setRealFee(0); // 实际费用
        o.setEstimateGrabSecond(0); //预计接单时间 （秒）
        o.setEstimateReceiveSecond(0); //预计送单时间 （秒）
        o.setSyncCount(0); // 同步次数
        o.setOrderStatus(Status.LogisticsStatus.daipeisong.getValue());
        return o;
    }

    public static ExpressOrder getShopApiExpressOrder(String expressCode, String expressName, OrderInfo order){
        ExpressOrder o = new ExpressOrder();
        o.setOrderId(order.getId());
        o.setCreatedTime(new Date());
        o.setAppointType(0);
        o.setDeliveryType(1); // 1.帮我送 2.帮我取
        o.setExpressCode(expressCode);
        o.setExpressName(expressName);

        o.setOrderTime(new Date());
        o.setToMobile(order.getRecipientPhone());
        o.setToAddress(order.getRecipientAddress());
        o.setToLatitude(order.getLatitude());
        o.setToLongitude(order.getLongitude());
        o.setToReceiveName(order.getRecipientName());
        o.setToGeoType(order.getGeoType());
        o.setGoodType(7);
        o.setWeight(1d);
        o.setFee(0); //费用 (分） 下同
        o.setAddFee(0); // 小费
        o.setDistance(0); //距离 (米)
        o.setTotalAmount(0); // 总金额
        o.setCouponFee(0); // 优惠金额
        o.setRealFee(0); // 实际费用
        o.setEstimateGrabSecond(0); //预计接单时间 （秒）
        o.setEstimateReceiveSecond(0); //预计送单时间 （秒）
        o.setSyncCount(0); // 同步次数
        o.setOrderStatus(Status.LogisticsStatus.daipeisong.getValue());
        return o;
    }


    public static ExpressOrder getExpressOrder(ShopInfoExpress express, OrderInfo order){
        ExpressOrder o = new ExpressOrder();
        o.setOrderId(order.getId());
        o.setExpressCode(express.getExpressCode());
        o.setExpressOrderNo("90"+StringUtil.createId(10)); // 本地物流订单号
        o.setCreatedTime(new Date());
        o.setCityName(express.getCityName());
        o.setAppointType(0);
        o.setStoreId(express.getStoreId());
        o.setShopId(express.getShopId());
        o.setDeliveryType(1); // 1.帮我送 2.帮我取
        o.setFromAddress(express.getAddress());
        o.setFromAddressDetail(express.getAddressDetail());
        o.setFromSenderName(express.getStoreName());
        o.setFromMobile(express.getContactsMobile());
        o.setFromLatitude(express.getLatitude());
        o.setFromLongitude(express.getLongitude());
        o.setFromGeoType(express.getGeoType());
        o.setToMobile(order.getRecipientPhone());
        o.setToAddress(order.getRecipientAddress());
        o.setToLatitude(order.getLatitude());
        o.setToLongitude(order.getLongitude());
        o.setToReceiveName(order.getRecipientName());
        o.setToGeoType(order.getGeoType());
        o.setGoodType(7);
        o.setWeight(1d);
        o.setFee(0); //费用 (分） 下同
        o.setAddFee(0); // 小费
        o.setDistance(0); //距离 (米)
        o.setTotalAmount(0); // 总金额
        o.setCouponFee(0); // 优惠金额
        o.setRealFee(0); // 实际费用
        o.setPunishAmount(0); // 取消配送罚金 (分)
        o.setDynamicFee(0); // 平台补贴
        o.setBackFee(0); //取消订单返回金额
        o.setEstimateGrabSecond(0); //预计接单时间 （秒）
        o.setEstimateReceiveSecond(0); //预计送单时间 （秒）
        o.setSyncCount(0); // 同步次数
        o.setExpressType(1); // 同城物流
        o.setOrderStatus(Status.LogisticsStatus.daipeisong.getValue());
        return o;
    }

    public static Integer expressLength(String arrayString){
        JSONArray array = JSONArray.parseArray(arrayString);
        return array.size();

    }

    // 只有礼品
    public static boolean onlyLipin(String arrayString){

        try{
            JSONArray array = JSONArray.parseArray(arrayString);
            if(array.size()==1){
                Integer type = array.getInteger(0);
                Status.ExpressType expressType =  Status.ExpressType.fromValue(type);
                if(expressType==Status.ExpressType.lipin){
                    return true;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return false;
    }

}
